﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xyz.Service;
using Xyz.Service.DataTransferObjects;
using System.Data;
using System.Data.SqlClient;

namespace Xyz.Web.Admin.Flight
{
    public partial class FlightSchedule : System.Web.UI.Page
    {
        FlightService fService = new FlightService();

        protected void Page_Load(object sender, EventArgs e)
        {  
        }

        private DataTable GetDataSource(string flightNumber)
        {
            DataTable db = CreateEmptyDTable();
            List<FlightDTO> flights = null;

            if (string.IsNullOrEmpty(flightNumber))
                flights = fService.GetFlights();
            else
                flights = fService.GetFlightsByFlightCode(flightNumber);

            foreach (FlightDTO f in flights)
            {
                DataRow dRow;
                dRow = db.NewRow();
                dRow["FlightCode"] = f.FlightCode;
                dRow["DepartureAirport"] = f.DepartureAirport;
                dRow["ArrivalAirport"] = f.ArrivalAirport;
                dRow["DepartureTerminal"] = f.DepartureTerminal;
                dRow["ArrivalTerminal"] = f.ArrivalTerminal;
                dRow["DepartureDateTime"] = f.DepartureDateTime;
                dRow["ArrivalDateTime"] = f.ArrivalDateTime;
                dRow["FlightStatus"] = f.FlightStatus;
                dRow["Distance"] = f.Distance;
                dRow["TaxSurcharge"] = f.TaxSurcharge;
                dRow["FlightId"] = f.FlightId;
                db.Rows.Add(dRow);
            }

            return db;
        }

        private DataTable CreateEmptyDTable()
        {
            DataTable dTable = new DataTable();

            DataColumn dColumn;
            dColumn = new DataColumn("FlightCode", System.Type.GetType("System.String"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("DepartureAirport", System.Type.GetType("System.String"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("ArrivalAirport", System.Type.GetType("System.String"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("DepartureTerminal", System.Type.GetType("System.String"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("ArrivalTerminal", System.Type.GetType("System.String"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("DepartureDateTime", System.Type.GetType("System.DateTime"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("ArrivalDateTime", System.Type.GetType("System.DateTime"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("FlightStatus", System.Type.GetType("System.String"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("Distance", System.Type.GetType("System.String"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("TaxSurcharge", System.Type.GetType("System.Decimal"));
            dTable.Columns.Add(dColumn);

            dColumn = new DataColumn("FlightId", System.Type.GetType("System.Int32"));
            dTable.Columns.Add(dColumn);

            dTable.PrimaryKey = new DataColumn[] { dColumn };

            return dTable;
        }


        protected void GridViewFlight_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            
        }

        protected void GridViewFlight_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        //fService.DeleteFlight(Convert.ToInt32(GridViewFlight.SelectedRow.Cells[0].Text));
        }

        protected void GridViewFlight_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewFlight.PageIndex = e.NewPageIndex;
            GridViewFlight.DataSource = GetDataSource(txtFlightNumber.Text.Trim());
            GridViewFlight.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            String flightNumber = txtFlightNumber.Text.Trim();
            GridViewFlight.DataSource = GetDataSource(flightNumber);
            GridViewFlight.DataBind();
        }

        protected void GridViewFlight_RowDataBound(object sender, GridViewRowEventArgs e)
        { 
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               GridViewFlight.HeaderRow.Cells[6].HorizontalAlign = HorizontalAlign.Right; 
            }

        }
    }
}